@page "/_Admin/FrameworkRole"
@using WalkingTec.Mvvm.Mvc.Admin.ViewModels.FrameworkRoleVMs;
@inherits BasePage
@attribute [ActionDescription("MenuKey.RoleManagement", "WalkingTec.Mvvm.Admin.Api,FrameworkRole")]

<WTSearchPanel OnSearch="@DoSearch">
    <ValidateForm Model="@_searchModel">
        <Row ItemsPerRow="ItemsPerRow.Two" RowType="RowType.Inline">
            <BootstrapInput @bind-Value="@_searchModel.RoleCode" />
            <BootstrapInput @bind-Value="@_searchModel.RoleName" />
        </Row>
    </ValidateForm>
</WTSearchPanel>

<Table @ref="_dataTable" 
       TItem="FrameworkRole" 
       OnQueryAsync="OnSearch" 
       IsPagination="true" 
       IsStriped="true" 
       IsBordered="true" 
       ShowRefresh="false"
       ShowToolbar="true" 
       IsMultipleSelect="true" 
       ShowExtendButtons="true" 
       ShowExtendDeleteButton="false"
       ShowExtendEditButton="false"
       ShowDefaultButtons="false" style="margin-top:10px;">
    <TableColumns>
        <TableColumn @bind-Field="@context.RoleCode" Width="180" />
        <TableColumn @bind-Field="@context.RoleName" />
        <TableColumn @bind-Field="@context.RoleRemark" />

    </TableColumns>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/_FrameworkRole/Add"))
        {
            <TableToolbarButton TItem="FrameworkRole" Color="Color.Primary" Icon="fa fa-fw fa-plus" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="OnCreateClick" />
        }
        @if (IsAccessable("/api/_FrameworkRole/BatchDelete"))
        {
            <TableToolbarPopconfirmButton TItem="FrameworkRole" Color="Color.Primary"
                                          Icon="fa fa-fw fa-trash-o" Text="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                                          OnConfirm="@OnBatchDeleteClick" Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]"
                                          ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]" ConfirmButtonColor="Color.Danger" />
        }
    </TableToolbarTemplate>
    <RowButtonTemplate>
            @if (IsAccessable("/api/_FrameworkRole/Edit"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Success" Icon="fa fa-edit" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="() => OnEditClick(context)" />
            }
            @if (IsAccessable("/api/_FrameworkRole/BatchDelete"))
            {
                <PopConfirmButton Icon="fa fa-trash-o" Text="@WtmBlazor.Localizer["Sys.Delete"]" OnConfirm="() => OnDeleteClick(context)" Color="Color.Danger" Size="Size.ExtraSmall"
                                  Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]" ConfirmButtonColor="Color.Danger" />
            }
            @if (IsAccessable("/api/_FrameworkRole/EditPrivilege"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Warning" Icon="fa fa-key" Text="@WtmBlazor.Localizer["_Admin.PageFunction"]" OnClick="() => OnPageFunctionClick(context)" />
            }
    </RowButtonTemplate>
</Table>

@code{

    private readonly FrameworkRoleSearcher _searchModel = new FrameworkRoleSearcher();
    private Table<FrameworkRole> _dataTable;

    private async Task<QueryData<FrameworkRole>> OnSearch(QueryPageOptions opts)
    {
        return await StartSearch<FrameworkRole>("/api/_FrameworkRole/Search", _searchModel, opts);
    }

    private void DoSearch()
    {
        _dataTable.QueryAsync();
    }

    private async Task OnCreateClick(IEnumerable<FrameworkRole> items)
    {
        if (await OpenDialog<Create>(WtmBlazor.Localizer["Sys.Create"]) == DialogResult.Yes)
        {
            await _dataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(FrameworkRole item)
    {
        if (await OpenDialog<Edit>(WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString()) == DialogResult.Yes)
        {
            await _dataTable.QueryAsync();
        }
    }

    private async Task OnPageFunctionClick(FrameworkRole item)
    {
        await OpenDialog<PageFunction>(WtmBlazor.Localizer["_Admin.PageFunction"], x => x.id == item.ID.ToString());
    }


    private async Task OnBatchDeleteClick()
    {
        if (_dataTable.SelectedRows?.Any() == true)
        {
            await PostsData(_dataTable.SelectedRows.Select(x => x.ID).ToList(), $"/api/_FrameworkRole/batchDelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await _dataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnDeleteClick(FrameworkRole item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/_FrameworkRole/batchDelete", (s) => "Sys.OprationSuccess");
        await _dataTable.QueryAsync();
    }
}
